import {Alert, Snackbar, useTheme} from "@mui/material";
import * as React from "react";
import {SnackbarCloseReason} from "@mui/material/Snackbar/Snackbar";
import {FC} from "react";
import {OverridableStringUnion} from "@mui/types";
import {AlertColor, AlertPropsColorOverrides} from "@mui/material/Alert/Alert";

interface DGToastProps {
    state: {
        state: boolean,
        severity: OverridableStringUnion<AlertColor, AlertPropsColorOverrides> | undefined,
        text: string | "" | undefined | null
    },
    close: () => void
}

const DGToast: FC<DGToastProps> = ({state, close}) => {

    // 查询模式
    const mode = useTheme().palette.mode === "light";
    // 关闭函数
    const handleClose = (_: React.SyntheticEvent | Event, reason: SnackbarCloseReason) => {
        if (reason === 'clickaway') return 0;
        close();
    }

    return (<Snackbar
        open={state.state}
        onClose={handleClose}
        autoHideDuration={2000}
        style={{marginBottom: "20px", pointerEvents: "none"}}
        anchorOrigin={{vertical: 'bottom', horizontal: 'center'}}>
        {/* 警告组件 */}
        <Alert severity={state.severity} variant={mode ? "filled" : "standard"} sx={{width: '100%'}}>
            {state.text}
        </Alert>
    </Snackbar>);
}
export default DGToast;
